// 1.下载 2.导入  3.注册全局组件  4.创建实例对象 5.导出并挂载到Vue实例
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login'
import auth from '@/utils/token'
import { Message } from 'element-ui'
import NotFind from '@/views/NotFind.vue'
import Layout from '@/views/Layout.vue'
import Article from '@/views/article'
import Home from '@/views/home'
import Publish from '@/views/publish'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      component: Layout,
      children: [
        { path: '/', component: Home },
        { path: 'article', component: Article },
        { path: 'publish/:id?', component: Publish }
      ]
    },
    { path: '*', component: NotFind }
  ]
})

router.beforeEach((to, from, next) => {
  // console.log({ to, from, next })
  const token = auth.getToken()
  if (to.path !== '/login' && !token) {
    Message.error('信息失效, 请重新登录')
    next('/login')
  } else {
    next()
  }
})

export default router
